package com.sptpc.demo1.controller;

import com.sptpc.demo1.model.Drink;
import com.sptpc.demo1.requests.ModifyDrinkRequest;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.Money;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/drink")
public class DrinkController {
    // get /drink/
    //@RequestMapping(value = "/", method= RequestMethod.GET)
    @GetMapping("/")
   public  List<Drink> getAll() {
      //后期从数据库查询
        List<Drink> results = new ArrayList<>();
        results.add(Drink.builder().id(1)
                .name("茉莉绿茶").price(Money.parse("USD 8.00"))
                .build());
        results.add(Drink.builder().id(2)
                .name("蜜桃四季春").price(Money.parse("USD 8.00"))
                .build());
return results;
    }

    /**
    * 查询指定饮品
     * get /drink/1
     * get /drink/?id=1
     * 日志 log4j
    */
    @GetMapping("/{id}")
    public Drink  getDrink(@PathVariable("id") Integer id){
        log.info("要查询的饮品id:{}",id);
        //后期从数据库查询
        Drink drink = Drink.builder().id(1)
                .name("茉莉绿茶")
                .price(Money.parse("USD 8.00"))
                .build();
        return  drink;
    }
    /**
    * 新建
    * post /drink/ {"name": "奶茶","price": "8:00"}
    * 传参思路：
    * 1、直接使用实体类
     * 2、根据前端页面编写TO类
     */
    @PostMapping("/")
  public Drink addNewDrink(@RequestBody Drink  drink){
        log.info("新增饮品：{}",drink);
        //后期insert数据库
        Drink drink1 = Drink.builder().id(1)
                .name("茉莉绿茶")
                .price(Money.parse("USD 8.00"))
                .build();
        return  drink1;
  }
    /**
     * 修改
     * put /drink/ {"id": "1","name":"新名称","price":9:00}
     */
    @PutMapping("/")
    public Drink modifyDrink(@RequestBody ModifyDrinkRequest request){
        log.info("要修改的饮品：{}",request);
        //后期update数据库
        Drink addDrink = Drink.builder().id(1)
                .name("茉莉绿茶")
                .price(Money.parse("USD 8.00"))
                .build();
        return  addDrink;
    }
    /**
     * delete/drink/1
     */
    @DeleteMapping("/{id}")
    public  String deleteDrink(@PathVariable Integer id){
        log.info("要删除的饮品id：{}",id);
        return "删除操作成功";
    }
}
